package com.hong610.config;

import com.hong610.listener.CleanupListener;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.util.EventListener;

/**
 * Created by Hong on 2017/2/9.
 */
@Configuration
public class ApplicationConfig {

    /**
     * 监听器
     */
    @Bean
    public ServletListenerRegistrationBean<EventListener> getDemoListener() {
        ServletListenerRegistrationBean<EventListener> registrationBean = new ServletListenerRegistrationBean<EventListener>();
        registrationBean.setListener(new CleanupListener());//内存监听，避免Quartz导致的内存泄露
        registrationBean.setListener(new QuartzInitializerListener());//启动项目自动启动Quartz任务
        return registrationBean;
    }

    /**
     * 数据源
     */
    @Bean
    DriverManagerDataSource driverManagerDataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/quartz_db?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("111111");
        return dataSource;
    }

    /**
     * JDBCTemplate
     */
    @Bean
    JdbcTemplate jdbcTemplate(){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(driverManagerDataSource());
        return jdbcTemplate;
    }
}
